import WxValidate from '../../utils/WxValidate';
const App = getApp();
const api = require('../../utils/api.js');
Page({
  data: {
    relationList: ['子女', '爱人', '亲属', '朋友', '朋友子女'],
    relationIndex: 0,
    birthday: '1992-10-12',
    name:'',
    idcard:'',
  },

  onLoad() {
    this.initValidate()
  },

  initValidate() {
    const rules = {
      name: {
        required: true,
      },
      idCard: {
        required: true,
        idcard: true,
      }
    };
    const messages = {
      name: {
        required: '请输入姓名',
      },
      idCard: {
        required: '请输入身份证号',
        idcard: '请输入正确的身份证号'
      }
    };
    this.WxValidate = new WxValidate(rules, messages)
  },

  submitData: function (event) {
    if (!this.WxValidate.checkForm(event.detail.value)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
      return false
    }
    let res = event.detail.value
    res.userId = wx.getStorageSync('userId')
    wx.showLoading({
      title: '添加伙伴中',
    });
    api.addPartner({
      res,
      success: (res) => {
        wx.hideLoading({
          success: (res) => {},
        })
        wx.navigateBack();
      },
      fail: (res) => {
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showToast({
          title: '网络异常',
        })
      }
    })
  },

  bindRelationChange: function (event) {
    this.setData({
      relationIndex: event.detail.value
    })
  },

  bindBirthdayChange: function (event) {
    this.setData({
      birthday: event.detail.value
    })
  },
})